package com.spa.domain.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.application.command.admin.AdminUserAddCommand;
import com.spa.application.command.admin.AdminUserDelCommand;
import com.spa.application.command.admin.AdminUserEditCommand;
import com.spa.domain.entity.Shop;
import com.spa.domain.entity.SysRole;
import com.spa.infrastructure.repository.po.RoleShopRefPO;
import com.spa.infrastructure.repository.service.RoleShopRefRepositoryService;
import com.spa.infrastructure.util.PasswdUtils;
import com.spa.domain.entity.SysUserEntity;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.mapper.SysUserMapperStruct;
import com.spa.infrastructure.repository.condition.SysUserCondition;
import com.spa.infrastructure.repository.po.SysUserPO;
import com.spa.infrastructure.repository.service.SysUserRepositoryService;
import com.spa.infrastructure.util.SessionUtil;
import com.spa.infrastructure.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 系统用户  domain
 *
 * @author: lig
 * @date: 2023-12-25
 */
@Service
public class SysAdminUserService {

    @Resource
    private SysUserRepositoryService sysUserRepositoryService;
    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private RoleShopRefRepositoryService roleShopRefRepositoryService;

    public boolean addUser(SysUserPO sysUserPo) {
        sysUserPo.setDelFlag(WhetherTypeEnum.$0.getCode().toString());
        return sysUserRepositoryService.save(sysUserPo);
    }

    @Transactional
    public SysUserPO addUser(AdminUserAddCommand command) {
        SysUserPO sysUserPo = new SysUserPO();
        sysUserPo.setUserName(command.getUsername());
        sysUserPo.setNickName(command.getName());
        sysUserPo.setPassword(PasswdUtils.encryptPassword("yuyuelaile"));
        sysUserPo.setDelFlag(WhetherTypeEnum.$0.getCode().toString());
        sysUserPo.setRoleId(Long.valueOf(command.getRoleId()));
        sysUserRepositoryService.save(sysUserPo);
        return sysUserPo;
    }

    @Transactional
    public boolean delUser(AdminUserDelCommand command) {
        return sysUserRepositoryService.removeById(command.getUserId());
    }

    @Transactional
    public SysUserPO editUser(AdminUserEditCommand command) {
        SysUserPO sysUserPo = sysUserRepositoryService.getById(command.getUserId());
        // 更改密码
        if (StringUtils.isNotBlank(command.getPassword())){
            sysUserPo.setPassword(PasswdUtils.encryptPassword(command.getPassword()));
        }
        sysUserRepositoryService.updateById(sysUserPo);
        return sysUserPo;
    }

    public Page<SysUserEntity> pageUser(SysUserCondition condition) {
        Page<SysUserEntity> page = SysUserMapperStruct.INSTANCE.po2Entity4Page(sysUserRepositoryService.page(condition));
        List<SysRole> sysRoles = sysRoleService.listAllRole();
        Map<Long, SysRole> map = sysRoles.stream().collect(Collectors.toMap(SysRole::getRoleId, Function.identity()));
        if (CollUtil.isNotEmpty(page.getRecords())) {
            page.getRecords().stream().forEach(a -> {
                a.setRoleName(map.get(a.getRoleId()).getRoleName());
            });
        }
        return page;
    }

    public SysUserEntity infoById(Long userId) {
        SysUserEntity sysUserEntity = SysUserMapperStruct.INSTANCE.po2Entity(sysUserRepositoryService.getById(userId));
//        String roleId = permissionsService.getRolesForUser(String.valueOf(sysUserEntity.getUserId()));
//        if(StringUtils.isNotEmpty(roleId)){
//            sysUserEntity.setRoleId(Convert.toLong(roleId));
//            sysUserEntity.setRoleName(sysRoleService.infoById(sysUserEntity.getRoleId()).getRoleName());
//        }
        return sysUserEntity;
    }

    public SysUserEntity infoOneByUsername(String username) {
        SysUserCondition condition = new SysUserCondition();
        condition.setUsername(username);
        List<SysUserEntity> list = SysUserMapperStruct.INSTANCE.po2Entity4List(sysUserRepositoryService.list(condition));
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public List<Long> getMyDataPermission() {
        long userId = SessionUtil.getUser().getExtUserId();
        SysUserPO sysUserPO = sysUserRepositoryService.getById(userId);
        List<RoleShopRefPO> roleShopRefPOS = roleShopRefRepositoryService.listByRoleId(sysUserPO.getRoleId());
        return roleShopRefPOS.stream().map(RoleShopRefPO::getShopId).toList();
    }

}
